package livingarea.actions_impl;

import livingarea.actions.MoveType;
import livingarea.Members.Beast;
import livingarea.Members.Position;
import livingarea.other.Coordinates;
import livingarea.other.Direction;
import livingarea.other.Random;

/**
 * Class incapsulate
 * <code> move() </code> method and realized a simple move.
 *
 * @author Muzalevskiy Vitaliy
 */
public class SimpleMove implements MoveType {

    /**
     * <code> Beast </code> object that delegate object of current class.
     */
    private Beast beast;

    /**
     * Constructor for current class.
     *
     * @param beast - <code> Beast </code> object that delegate object of
     * current class.
     */
    public SimpleMove(Beast beast) {
        this.beast = beast;
    }

    /**
     * This method made beast to make a move in random direction to one step.
     */
    @Override
    public void move() {
        int value = getRandomValue();
        Direction direction = Coordinates.DIRECTIONS[value];
        int x = beast.getPosition().getX();
        int y = beast.getPosition().getY();;
        switch (direction) {
            case NORTH:
                if (beast.getPosition().getY() < beast.getLivingArea()
                        .getCoordinates().getHeight()) {
                    x = beast.getPosition().getX();
                    y = beast.getPosition().getY() + 1;
                }
                break;
            case SOUTH:
                if (beast.getPosition().getY() > 0) {
                    x = beast.getPosition().getX();
                    y = beast.getPosition().getY() - 1;
                }
                break;
            case NORTH_EAST:
                if (beast.getPosition().getX() < beast.getLivingArea()
                        .getCoordinates().getLength()
                        && beast.getPosition().getY() < beast.getLivingArea()
                        .getCoordinates().getHeight()) {
                    
                    if ((beast.getPosition().getX() % 2) == 0
                            || beast.getPosition().getX() == 0) {
                        y = beast.getPosition().getY();
                    } else {
                        y = beast.getPosition().getY() + 1;
                    }
                    x = beast.getPosition().getX() + 1;

                }
                break;
            case NORTH_WEST:
                if (beast.getPosition().getX() > 0                         
                        && beast.getPosition().getY() < beast.getLivingArea()
                        .getCoordinates().getHeight()) {
                    if ((beast.getPosition().getX() % 2) == 0
                            || beast.getPosition().getX() == 0) {
                        y = beast.getPosition().getY();
                    } else {
                        y = beast.getPosition().getY() + 1;
                    }
                    x = beast.getPosition().getX() - 1;
                }
                break;
            case SOUTH_EAST:
                if (beast.getPosition().getX() < beast.getLivingArea()
                        .getCoordinates().getLength()                         
                        && beast.getPosition().getY() > 0) {
                    if ((beast.getPosition().getX() % 2) == 0
                            || beast.getPosition().getX() == 0) {
                        y = beast.getPosition().getY();
                    } else {
                        y = beast.getPosition().getY() - 1;
                    }
                    x = beast.getPosition().getX() + 1;
                }
                break;
            case SOUTH_WEST:
                if (beast.getPosition().getX() > 0                        
                        && beast.getPosition().getY() > 0) {
                    if ((beast.getPosition().getX() % 2) == 0
                            || beast.getPosition().getX() == 0) {
                        y = beast.getPosition().getY();
                    } else {
                        y = beast.getPosition().getY() - 1;
                    }
                    x = beast.getPosition().getX() - 1;
                }
                break;
            default:
                throw new RuntimeException();
        }
        if (!beast.getLivingArea().positionIsBlocked(new Position(x, y))) {
            beast.changePosition(x, y);
        }
    }

    public int getRandomValue() {
        return (int) (new Random().getRandomValue(Coordinates.DIRECTIONS.length));
    }
}
